import { Injectable } from "@nestjs/common";
import * as path from "path"
import * as moment from "moment"
import * as fs from "fs-extra"
import IHelper from "@/services/helper.service";
import IConstants from "@/core/constants";
import { ConfigService } from "@nestjs/config";

@Injectable()
export class UeditorService {
  constructor(
    private readonly configService: ConfigService
  ) {}
  handlerConfig() {
    return {"imageActionName":"uploadimage","imageFieldName":"upfile","imageMaxSize":2097152,"imageAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp"],"imageCompressEnable":true,"imageCompressBorder":1600,"imageInsertAlign":"none","imageUrlPrefix":"","imagePathFormat":"\/upload\/ueditor\/image\/{yyyy}{mm}{dd}\/{time}{rand:6}","scrawlActionName":"uploadscrawl","scrawlFieldName":"upfile","scrawlPathFormat":"\/upload\/ueditor\/image\/{yyyy}{mm}{dd}\/{time}{rand:6}","scrawlMaxSize":2097152,"scrawlUrlPrefix":"","scrawlInsertAlign":"none","snapscreenActionName":"uploadimage","snapscreenPathFormat":"\/upload\/ueditor\/image\/{yyyy}{mm}{dd}\/{time}{rand:6}","snapscreenUrlPrefix":"","snapscreenInsertAlign":"none","catcherLocalDomain":["127.0.0.1","localhost","img.baidu.com"],"catcherActionName":"catchimage","catcherFieldName":"source","catcherPathFormat":"\/upload\/ueditor\/image\/{yyyy}{mm}{dd}\/{time}{rand:6}","catcherUrlPrefix":"","catcherMaxSize":2097152,"catcherAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp"],"videoActionName":"uploadvideo","videoFieldName":"upfile","videoPathFormat":"\/upload\/ueditor\/video\/{yyyy}{mm}{dd}\/{time}{rand:6}","videoUrlPrefix":"","videoMaxSize":102400000,"videoAllowFiles":[".flv",".swf",".mkv",".avi",".rm",".rmvb",".mpeg",".mpg",".ogg",".ogv",".mov",".wmv",".mp4",".webm",".mp3",".wav",".mid"],"fileActionName":"uploadfile","fileFieldName":"upfile","filePathFormat":"\/upload\/ueditor\/file\/{yyyy}{mm}{dd}\/{time}{rand:6}","fileUrlPrefix":"","fileMaxSize":51200000,"fileAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp",".flv",".swf",".mkv",".avi",".rm",".rmvb",".mpeg",".mpg",".ogg",".ogv",".mov",".wmv",".mp4",".webm",".mp3",".wav",".mid",".rar",".zip",".tar",".gz",".7z",".bz2",".cab",".iso",".doc",".docx",".xls",".xlsx",".ppt",".pptx",".pdf",".txt",".md",".xml"],"imageManagerActionName":"listimage","imageManagerListPath":"\/upload\/ueditor\/image\/","imageManagerListSize":20,"imageManagerUrlPrefix":"","imageManagerInsertAlign":"none","imageManagerAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp"],"fileManagerActionName":"listfile","fileManagerListPath":"\/upload\/ueditor\/file\/","fileManagerUrlPrefix":"","fileManagerListSize":20,"fileManagerAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp",".flv",".swf",".mkv",".avi",".rm",".rmvb",".mpeg",".mpg",".ogg",".ogv",".mov",".wmv",".mp4",".webm",".mp3",".wav",".mid",".rar",".zip",".tar",".gz",".7z",".bz2",".cab",".iso",".doc",".docx",".xls",".xlsx",".ppt",".pptx",".pdf",".txt",".md",".xml"]}
  }

  handlerUpload(file: Express.Multer.File) {
    const extname = path.extname(file.originalname);
    const relativePath = path.join(
      "uploads",
      moment().format("YYYY/MM/DD"),
      `${IHelper.hash(32)}${extname}`,
    );
    const absoluteFilepath = path.join(IConstants.publicDir, relativePath);
    fs.ensureFileSync(absoluteFilepath);
    fs.writeFileSync(absoluteFilepath, file.buffer);
    const url = path.join(
      this.configService.get("STATIC_PREFIX", "static"),
      relativePath,
    );
    const finalUrl = ["", ...url.split(path.sep)].join("/");
    return {
      "state":"SUCCESS",
      "url": finalUrl,
      "title":"1723196111212829.png",
      "original": file.originalname,
      "type": extname,
      "size": file.size
    }
  }
}